package cn.micro.system.manage.annotate.impl;

import cn.micro.system.manage.annotate.UniqueProfession;
import cn.micro.system.manage.entity.SysDimProfession;
import cn.micro.system.manage.service.SysDimProfessionService;

import javax.annotation.Resource;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.lang.annotation.Annotation;
import java.util.function.Predicate;

public class ProfessionValidation  <T extends Annotation> implements ConstraintValidator<T, SysDimProfession> {

    protected Predicate<SysDimProfession> predicate = c -> true;

    @Resource
    protected SysDimProfessionService sysDimProfessionService;

    @Override
    public boolean isValid(SysDimProfession sysDimProfession, ConstraintValidatorContext constraintValidatorContext) {
        return sysDimProfessionService == null || predicate.test(sysDimProfession);
    }

    /**
     * 校验角色编码或者角色名称是否重复
     */
    public static class UniqueProfessionValidate extends ProfessionValidation<UniqueProfession>{
        @Override
        public void initialize(UniqueProfession constraintAnnotation) {
            predicate = c -> sysDimProfessionService.getByCodeOrName(c).isEmpty();
        }
    }
}
